package org.permission.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.permission.dao.SysPermissionDao;
import org.permission.dao.SysRoleDao;
import org.permission.pojo.SysRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/sysRole")
public class SysRoleController {
	@Autowired
	private SysRoleDao sysRoleDao;
	@Autowired
	private SysPermissionDao sysPermissionDao;

	public void setSysPermissionDao(SysPermissionDao sysPermissionDao) {
		this.sysPermissionDao = sysPermissionDao;
	}

	public void setSysRoleDao(SysRoleDao sysRoleDao) {
		this.sysRoleDao = sysRoleDao;
	}
	
	@RequestMapping("/searchPers")
	@ResponseBody
	public List<Integer> searchPers(Integer rid) throws Exception {
		return sysPermissionDao.getPersByRoleId(rid);
	}
	
	@RequestMapping("/assignRights")
	@ResponseBody
	public Map<String, Object> assignRights(Integer rid, Integer[] perIds) {
		Map<String, Object> map = new HashMap<>();
		
		sysRoleDao.deleteByRoleId(rid);
		sysRoleDao.assignRights(rid, perIds);
		
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/all")
	@ResponseBody
	public List<SysRole> searchAll() throws Exception {
		return sysRoleDao.getAll();
	}
	
	@RequestMapping("/index")
	public String index() throws Exception {
		return "sysRole/index";
	}
	
	@RequestMapping("/assign")
	public String assign() throws Exception {
		return "sysRole/assignPermission";
	}
	
	@RequestMapping("/list")
	@ResponseBody
	public Map<String, Object> list(@RequestParam(defaultValue="1") Integer page,@RequestParam(defaultValue="10") Integer rows,
			@RequestParam(defaultValue="asc") String order,@RequestParam(defaultValue="id") String sort,
			SysRole condition) throws Exception {
		Map<String, Object> map = new HashMap<>();
		
		int start = (page - 1) * rows;
		List<SysRole> list = sysRoleDao.getConditionList(start, rows, condition, sort, order);
		int total = sysRoleDao.getConditionCount(condition);
		
		map.put("total", total);
		map.put("rows", list);
		
		return map;
	}
	
	@RequestMapping(value="/form",method=RequestMethod.GET)
	public String add() throws Exception {
		return "sysRole/form";
	}
	
	@RequestMapping(value="/add",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> add(SysRole sysRole) throws Exception {
		Map<String, Object> map = new HashMap<>();
		sysRoleDao.add(sysRole);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping(value="/batchDelete")
	@ResponseBody
	public Map<String, Object> batchDelete(Integer[] ids) throws Exception {
		Map<String, Object> map = new HashMap<>();
		sysRoleDao.batchDelete(ids);
		map.put("result", true);
		return map;
	}
	
	@RequestMapping("/view/{id}")
	@ResponseBody
	public SysRole view(@PathVariable Integer id) throws Exception {
		return sysRoleDao.get(id);
	}
	
	@RequestMapping(value="/edit",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> edit(SysRole sysRole) throws Exception {
		Map<String, Object> map = new HashMap<>();
		sysRoleDao.update(sysRole);
		map.put("result", true);
		return map;
	} 
	
}
